package presentacion;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JToolBar;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Pedido extends JFrame {

	private JPanel contentPane;
	private final JToolBar toolBar = new JToolBar();
	private final JButton btnAadir = new JButton("Añadir");
	private final JButton btnModificar = new JButton("Eliminar");
	private final JScrollPane scrollPane = new JScrollPane();
	private final JButton btnEnviarPedido = new JButton("Enviar pedido");
	private final JTable table = new JTable();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Pedido frame = new Pedido();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Pedido() {
		
	
		//addWindowListener(new ThisWindowListener());
		setBounds(100, 100, 513, 303);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		MiPedidoTabla pedidotabla = new MiPedidoTabla();
		
		Object[] fila1= {  " ", " ", " ", " ", " ", " "};
		pedidotabla.aniadeFila(fila1);
		Object[] fila2= {  " ", " ", " ", " ", " ", " "};
		pedidotabla.aniadeFila(fila2);
		Object[] fila3= {  " ", " ", " ", " ", " ", " "};
		pedidotabla.aniadeFila(fila3);
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.setLayout(new BorderLayout(0, 0));
		contentPane.add(toolBar, BorderLayout.NORTH);
		contentPane.add(scrollPane);
		{
			contentPane.add(toolBar, BorderLayout.NORTH);
		}
		{
			btnAadir.addActionListener(new BtnAadirActionListener());
			toolBar.add(btnAadir);
		}
		{
			btnModificar.addActionListener(new BtnModificarActionListener());
			toolBar.add(btnModificar);
		}
		btnEnviarPedido.addActionListener(new BtnEnviarPedidoActionListener());
		
		toolBar.add(btnEnviarPedido);
		
		contentPane.add(scrollPane);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
				{null, null, null, null, null, null},
			},
			new String[] {
				"ID", "Color", "Cantidad", "Medidas (ancho x alto x profundo)", "Precio", "Descripcion"
			}
		) {
			Class[] columnTypes = new Class[] {
				Integer.class, String.class, Integer.class, String.class, Double.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		
		scrollPane.setViewportView(table);
		
	}
	

	private class BtnAadirActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			MiPedidoTabla pedidotabla =  (MiPedidoTabla) table.getModel();
			Object[] nuevaFila = {" ", " "," ", " "," ", " "};
			pedidotabla.aniadeFila(nuevaFila);
			pedidotabla.fireTableDataChanged();
		}
	}
	private class BtnModificarActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			MiPedidoTabla pedidotabla = (MiPedidoTabla) table.getModel();
			int n= table.getSelectedRow();
			if (n != -1) 
				pedidotabla.eliminaFila(table.getSelectedRow());
			pedidotabla.fireTableDataChanged();
		}
	}
	private class BtnEnviarPedidoActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(contentPane, "El almacen se encargara de prepararlo y enviarlo", "pedido listo para prapararlo", JOptionPane.PLAIN_MESSAGE);
			dispose();
		}
	}
}
